package com.letwal.cmcvnd.create.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.RevDataMnemonic

class MnemonicAdapter(private val dataList: List<RevDataMnemonic>) :
    RecyclerView.Adapter<MnemonicAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textAdpterMnemonicText: TextView =
            view.findViewById(R.id.textAdpterMnemonicText)
        var textAdpterMnemonicIndex: TextView =
            view.findViewById(R.id.textAdpterMnemonicIndex)
        var lineAdpterMnemonicBottom: View =
            view.findViewById(R.id.lineAdpterMnemonicBottom)
        var lineAdpterMnemonicRight: View =
            view.findViewById(R.id.lineAdpterMnemonicRight)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_mnemonic, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataMnemonic = dataList[position]
        holder.textAdpterMnemonicText.text = data.content

        //mode 3 = 0时，右边线隐藏
        val mod3 = (position+1) % 3
        if ((position > 0) and (mod3 == 0)) {
            holder.lineAdpterMnemonicRight.visibility = View.GONE
        } else {
            holder.lineAdpterMnemonicRight.visibility = View.VISIBLE
        }

        //最后3个时，底线隐藏
        val isLast1 = (position == (dataList.size - 1))
        val isLast2 = (position == (dataList.size - 2))
        val isLast3 = (position == (dataList.size - 3))
        if (isLast1 or isLast2 or isLast3) {
            holder.lineAdpterMnemonicBottom.visibility = View.GONE
        } else {
            holder.lineAdpterMnemonicBottom.visibility = View.VISIBLE
        }

        if (data.index > 9) {
            holder.textAdpterMnemonicIndex.text = data.index.toString()
        } else {
            holder.textAdpterMnemonicIndex.text = "0${data.index}"
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}